package com.nari.ies.ne.nw.server.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class PropUtil {
	private static Properties properties = new Properties();

	public static Properties getProperties() {
		/*
		 * 获取指定目录下配置文件
		 */
		InputStream in = null;
		File file = new File("/home/d5000/yangjiang/conf/filepath.properties");
		//File file = new File("E:/filepath1.properties");
		try {
			if (!file.exists()) {
				System.out.println(
						"配置文件不存在,无法读取，请在/home/d5000/yangjiang/conf/filepath.properties,变量filepath=需要放置RB文件的路径");
			}
			in = new FileInputStream(file);
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IoUtil.close(in);
		}
		return properties;
	}

	/**
	 * 修改配置文件指定key的值
	 * 
	 * @param key
	 * @param value
	 */
	public static void updateProperties(String key, String value) {
		FileOutputStream oFile = null;
		InputStream in = null;
		File file = new File("/home/d5000/yangjiang/conf/filepath.properties");
		//File file = new File("E:/filepath1.properties");
		try {
			if (!file.exists()) {
				System.out.println(
						"配置文件不存在,无法读取，请在/home/d5000/yangjiang/conf/filepath.properties,变量filepath=需要放置RB文件的路径");
			}
			in = new FileInputStream(file);
			properties.load(in);
			properties.setProperty(key, value);
			oFile = new FileOutputStream(file);
			properties.store(oFile, "The New properties file");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IoUtil.close(in);
			IoUtil.close(oFile);
		}
	}

	public static void moveFile(String localPath, String movePath) throws IOException {
		File moveFile = new File(movePath);
		// 判断是否有文件，有就删除
		if (moveFile.exists()) {
			moveFile.delete();
		}
		Files.move(Paths.get(localPath), Paths.get(movePath));
	}
}
